/*
 * @Author: your name
 * @Date: 2024-04-22 13:32:39
 * @LastEditTime: 2024-04-22 14:54:23
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \.leetcode\257.二叉树的所有路径.cpp
 */
/*
 * @lc app=leetcode.cn id=257 lang=cpp
 *
 * [257] 二叉树的所有路径
 */

// @lc code=start
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    void getString(TreeNode* root, string str, vector<string>& res){
        if(!root->left && !root->right){
            // 叶子节点
            str += to_string(root->val);
            // 到达叶子节点，加入数组
            res.push_back(str);
            return;
        }
        if(root->left){
            string left_str = str;
            left_str += to_string(root->val);
            left_str += "->";
            getString(root->left, left_str, res);
        }
        if(root->right){
            string right_str = str;
            right_str += to_string(root->val);
            right_str += "->";
            getString(root->right, right_str, res);
        }
    }
    vector<string> binaryTreePaths(TreeNode* root) {
        if(root == nullptr){
            return {};
        }
        vector<string> res;
        string s;
        getString(root, s, res);
        return res;
    }
};
// @lc code=end

